package com.sunjy.demo1.controller;

import com.alibaba.fastjson.JSON;
import com.sunjy.common.annotation.BeanValid;
import com.sunjy.common.base.BaseController;
import com.sunjy.common.domain.ApiResponseT;
import com.sunjy.demo1.form.UserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Auther: sunjy
 * @Date: 2018/10/18 14:26
 * @Description:
 */
@RestController
@RequestMapping("redis")
public class TestRedisController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @BeanValid()
    @PostMapping(value="test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponseT postUser(@RequestBody @Valid UserForm user, BindingResult bind) {

        stringRedisTemplate.opsForHash().put("StringHashredis","hash1","value1值");
        stringRedisTemplate.opsForHash().put("StringHashredis","hash2","value2");
        System.out.println(JSON.toJSON(stringRedisTemplate.opsForHash().get("StringHashredis","hash1")));
        stringRedisTemplate.opsForValue().set("stringvalue","工时费");
        System.out.println(stringRedisTemplate.opsForValue().get("stringvalue"));

        redisTemplate.opsForHash().put("hashredis","hash1","value1值");
        redisTemplate.opsForHash().put("hashredis","hash2","value2");
        System.out.println(JSON.toJSON(redisTemplate.opsForHash().get("hashredis","hash1")));
        redisTemplate.opsForValue().set("key","工时费");
        System.out.println(redisTemplate.opsForValue().get("key"));



        return ApiResponseT.success();
    }
}
